package com.scorpio.baselibrary.utils.glide;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import androidx.annotation.DrawableRes;
import androidx.annotation.Nullable;
import androidx.annotation.RawRes;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.request.RequestOptions;

import java.io.File;
import java.net.URL;

/**
 * created by zhud on 2018/10/25
 */
public class GlideUtil {
    public static void load(Context context, @Nullable Bitmap bitmap, ImageView imageView) {
        startLoad(Glide.with(context).load(bitmap), null, imageView);
    }

    public static void load(Context context, @Nullable Drawable drawable, ImageView imageView) {
        startLoad(Glide.with(context).load(drawable), null, imageView);
    }

    public static void load(Context context, @Nullable String string, ImageView imageView) {
        startLoad(Glide.with(context).load(string), null, imageView);
    }

    public static void load(Context context, @Nullable String string, ImageView imageView
            , @RawRes @DrawableRes Integer placeholderResourceId
            , @RawRes @DrawableRes Integer errorResourceId) {
        startLoad(Glide.with(context).load(string), null, imageView, placeholderResourceId, errorResourceId);
    }

    public static void load(Context context, @Nullable Uri uri, ImageView imageView) {
        startLoad(Glide.with(context).load(uri), null, imageView);
    }

    public static void load(Context context, @Nullable File file, ImageView imageView) {
        startLoad(Glide.with(context).load(file), null, imageView);
    }

    public static void load(Context context, @RawRes @DrawableRes @Nullable Integer resourceId, ImageView imageView) {
        startLoad(Glide.with(context).load(resourceId), null, imageView);
    }

    public static void load(Context context, @Nullable URL url, ImageView imageView) {
        startLoad(Glide.with(context).load(url), null, imageView);
    }

    public static void load(Context context, @Nullable Object model, ImageView imageView) {
        startLoad(Glide.with(context).load(model), null, imageView);
    }

    public static void load(Context context, @Nullable byte[] model, ImageView imageView) {
        startLoad(Glide.with(context).load(model), null, imageView);
    }

    private static void startLoad(RequestBuilder<Drawable> builder, RequestOptions options, ImageView imageView) {
        startLoad(builder, null, imageView, null, null);
    }

    public static void loadAppIcon(Context context, PackageInfo path, ImageView imageView
            , @RawRes @DrawableRes Integer placeholderResourceId
            , @RawRes @DrawableRes Integer errorResourceId) {
        startLoad(Glide.with(context).load(path), null, imageView, placeholderResourceId, errorResourceId);
    }

    private static void startLoad(RequestBuilder<Drawable> builder
            , RequestOptions options, ImageView imageView
            , @RawRes @DrawableRes Integer placeholderResourceId
            , @RawRes @DrawableRes Integer errorResourceId) {
        options = options == null ? new RequestOptions() : options;
        options.skipMemoryCache(false)
                .dontAnimate();
        if (placeholderResourceId != null) options.placeholder(placeholderResourceId);
        if (errorResourceId != null) options.error(errorResourceId);
        builder.apply(options).into(imageView);
    }
}
